package auto.app.recv;

import auto.app.config.DirectRabbitConfig;
import auto.app.service.EmailService;
import auto.app.service.ResultService;
import auto.app.util.PdfReport;
import auto.app.vo.TestReportVo;
import cn.hutool.core.util.IdUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
@RabbitListener(queues = DirectRabbitConfig.QUEUE_NAME_MAIL)//监听的队列名称
@Slf4j
public class MaILReceiver {
    @Autowired
    private ResultService resultService;

    @Autowired
    private EmailService emailService;

    @Value("${spring.file.path}")
    private String path;

    //执行测试合集
    @RabbitHandler
    public void process(Map testMessage) throws Exception {
        Integer collectionId = (int)testMessage.get("collectionId");
        List<String> mailUrlList = (List<String>)testMessage.get("mailUrlList");
        List<TestReportVo> testReportVos = resultService.export(collectionId);
        PdfReport report=new PdfReport();
        ByteArrayOutputStream byteArrayOutputStream = report.pdfReportExcult(testReportVos,"后台生成报告");
//        String filename="测试报告" + IdUtil.simpleUUID() + ".pdf";
//        //这里将测试报告存在了本地 不可取 需要修改
//        FileOutputStream fileOutputStream1 = new FileOutputStream(path + filename);
//        fileOutputStream1.write(byteArrayOutputStream.toByteArray());
        long time = new Date().getTime();
        for (String s : mailUrlList) {
            emailService.sendAttachmentsMailArray(s,"测试报告"+ LocalDateTime.now(),"测试报告"+ time+".pdf",byteArrayOutputStream);
        }
    }
}
